package com.example.expandablelistview.gesture;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;

import com.example.expandablelistview.R;

public class GestureDump extends Activity {
	ArrayList<String> arGesture;
	TextView mResult;
	GestureDetector mDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arGesture = new ArrayList<String>();
		
		setContentView(R.layout.gesture_gesturedump);
		mResult = (TextView) findViewById(R.id.gesturedump_result);
		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setOnDoubleTapListener(mDoubleTapListener);
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	};
	
	OnGestureListener mGestureListener =  new OnGestureListener() {
		public boolean onSingleTapUp(MotionEvent e) {
			apppendText("onSingleTapUp");
			return false;
		}
		public void onShowPress(MotionEvent e) {
			apppendText("onShowPress");
		}
		
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			String text = String.format("Scroll : (%d, %d) -> (%d, %d) distance(%d,%d)",
					(int)e1.getX(),(int)e1.getY(), (int)e2.getX(),(int)e2.getY(),
					(int)distanceX, (int)distanceY
					);
			apppendText(text);
			return false;
		}
		
		public void onLongPress(MotionEvent e) {
			apppendText("onLongPress");
		}
		
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			String text = String.format("Fling : (%d,%d) -> (%d, %d) Velocity(%d,%d)",
					(int)e1.getX(),(int)e1.getY(), (int)e2.getX(),(int)e2.getY(),
					(int)velocityX, (int)velocityY
					);
			apppendText(text);
			return false;
		}
		
		public boolean onDown(MotionEvent e) {
			String text = String.format("onDown : %d,  %d", (int)e.getX(), (int)e.getY());
			apppendText(text);
			return false;
		}
	};
	
	OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener() {
		public boolean onSingleTapConfirmed(MotionEvent e) {
			apppendText("onSingleTapConfirmed");
			return false;
		}
		public boolean onDoubleTapEvent(MotionEvent e) {
			apppendText("onDoubleTapEvent");
			return false;
		}
		public boolean onDoubleTap(MotionEvent e) {
			apppendText("onDoubleTap");
			return false;
		}
	};
	
	private void apppendText(String text){
		if (arGesture.size()> 15){
			arGesture.remove(0);
		}
		arGesture.add(text);
		StringBuilder sb = new StringBuilder();
		for(String str : arGesture){
			sb.append(str+ "\n");
		}
		mResult.setText(sb.toString());
	}
}
